package com.weimei.conversionTreasure.weight;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.http.entity.SaveListBean;
import com.weimei.conversionTreasure.utils.ValidateUtils;
import com.weimei.conversionTreasure.viewModel.ItemItemVM;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class BottomAddRecordPop extends BottomPopupView {

    private TextView sure,cancel,add_record;
    private Context mContext;
    private EditText title;

    AddRecordBean tabOneEntity;

    private Click mClick;

    SaveListBean.DataDTO mData;

    public interface Click{
        void click(String s,String records);
    }

    public BottomAddRecordPop(@NonNull Context context, Click click) {
        super(context);
        this.mContext = context;
        this.mClick = click;
    }

    public BottomAddRecordPop(@NonNull Context context, Click click, SaveListBean.DataDTO bean) {
        super(context);
        this.mContext = context;
        this.mClick = click;
        this.mData = bean;
    }



    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_bottom_add_record;
    }

    @Override
    protected void onCreate() {
        sure = findViewById(R.id.sure);
        cancel = findViewById(R.id.cancel);
        add_record = findViewById(R.id.add_record);
        title = findViewById(R.id.title);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);

        cancel.setOnClickListener(view -> dismiss());
        AddRecordAdapter adapter = new AddRecordAdapter();
        recyclerView.setAdapter(adapter);


        if (ValidateUtils.isNotEmptyObjectOrString(mData)){
            title.setText(mData.getTitle());

            String record = mData.getRecordArr();
            Type type = new TypeToken<List<AddRecordBean>>() {}.getType();
            List<BottomAddRecordPop.AddRecordBean> list = new Gson().fromJson(record, type);
            for (BottomAddRecordPop.AddRecordBean bean1 : list) {
                tabOneEntity = new AddRecordBean();
                tabOneEntity.setName(bean1.getName());
                tabOneEntity.setWeight(bean1.getWeight());
                adapter.addData(tabOneEntity);
            }
        }else {
            tabOneEntity = new AddRecordBean();
            adapter.addData(tabOneEntity);
        }


        add_record.setOnClickListener(view -> {
            if (adapter.getData().size() > 10){
                ToastUtils.showShort("最多添加10条记录");
                return;
            }
            tabOneEntity = new AddRecordBean();
            adapter.addData(tabOneEntity);
        });





        sure.setOnClickListener(v -> {
            if (ValidateUtils.isEmptyString(title.getText().toString())){
                ToastUtils.showShort("请输入标题");
                return;
            }
            if (adapter.getData().size() != 0 &&
                    ValidateUtils.isNotEmptyString(adapter.getData().get(0).name)){
                String records = new Gson().toJson(adapter.getData());
                mClick.click(title.getText().toString(), records);
                Log.d("XXXX",records);
                dismiss();
            }else {
                ToastUtils.showShort("请输入相关记录");
            }
        });

        cancel.setOnClickListener(v -> dismiss());

    }

    public class AddRecordBean{
        private String name;
        private String weight;

        public String getName() {
            return name;
        }

        public String getWeight() {
            return weight;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setWeight(String weight) {
            this.weight = weight;
        }

        @Override
        public String toString() {
            return "TabOneEntity{" +
                    "name='" + name + '\'' +
                    ", weight='" + weight + '\'' +
                    '}';
        }
    }


    public class AddRecordAdapter extends BaseQuickAdapter<AddRecordBean, BaseViewHolder>{

        public AddRecordAdapter() {
            super(R.layout.item_add_record);
        }

        @Override
        protected void convert(@NonNull BaseViewHolder holder, AddRecordBean tabOneEntity) {
            holder.setText(R.id.num,getItemPosition(tabOneEntity)+1+"、");


            holder.setText(R.id.jiu_name,tabOneEntity.name);
            holder.setText(R.id.jiu_kg,tabOneEntity.weight);


            EditText jiuName = holder.getView(R.id.jiu_name);
            EditText jiuKg = holder.getView(R.id.jiu_kg);
            jiuName.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    tabOneEntity.setName(s.toString());
                }
            });

            jiuKg.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    tabOneEntity.setWeight(s.toString());
                }
            });


        }
    }





}
